

var app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
const {
  requestDataparams
} = require('../../utils/request.js');
Page({
  data: {
    currentTab: 0,
    btn_name:'',
    userPhone:'',
    userId:'',
    items: [
      {
        "pagePath": "pages/index/index",
        "text": "首页",
        "iconPath": applet_park_api_url_imgUrl + "/index.png",
        "selectedIconPath": applet_park_api_url_imgUrl + "/index_act.png",
        'readnum':0
      },
      {
        "pagePath": "/pages/find/find",
        "text": "发现",
        "iconPath": applet_park_api_url_imgUrl + "/fax.png",
        "selectedIconPath": applet_park_api_url_imgUrl + "/fax_act.png",
        'readnum': 0
      },
      {
        "pagePath": "/pages/msg/msg",
        "text": "消息",
        "iconPath":  applet_park_api_url_imgUrl + "/mag.png",
        "selectedIconPath": applet_park_api_url_imgUrl + "/mag_act.png",
        'readnum': 0
      },
      {
        "pagePath": "pages/my/my",
        "text": "我的",
        "iconPath":  applet_park_api_url_imgUrl + "/my.png",
        "selectedIconPath":  applet_park_api_url_imgUrl + "/my_act.png",
        'readnum': 0
      }
    ]
  },
  swichNav: function (e) {
    let that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
    if (e.target.dataset.current == 2){
      this.component_msg.loadData();
      
    }
    if (e.target.dataset.current == 3){
      this.component_my.initPage();
      
    }
    this.loadunreadNumber();
  },
  onLoad: function (options) {
    if(options.btn_name){
      this.setData({
        btn_name: options.btn_name
      })
    }
    // if(options.currentTab){
    //   this.setData({
    //     currentTab: options.currentTab
    //   })
    // }
    if(options.userPhone){
      this.setData({
        userPhone: options.userPhone
      })
    }
    if(options.userId){
      this.setData({
        userId: options.userId
      })
    }
    if(options.btn_name){//跳转访客首页
      wx.navigateTo({
        url: '../../packageA/pages/visitorIndex/visitorIndex?userPhone='+options.userPhone+'&userId='+options.userId+'&btn_name='+options.btn_name+'&startTime='+options.startTime+'&endTime='+options.endTime,
      })
    }
    this.component_index = this.selectComponent("#component_index")
    this.component_msg = this.selectComponent("#component_msg")
    this.component_my = this.selectComponent("#component_my")

    // setInterval(this.loadunreadNumber(), 3000)
    // setTimeOut(function () {
    //   //循环代码
    //   this.loadunreadNumber()
    // }, 1000)
    //setInterval(this.loadunreadNumber, 2000);
    // setTimeout(this.loadunreadNumber, 111000);
  },
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    //this.getCourseList();

    this.component_msg = this.selectComponent("#component_msg")
    this.component_index.loadData();
    // 
    this.component_msg.loadData();
    this.loadunreadNumber();
    wx.showToast({
      title: '刷新成功',
      icon: 'success',
      duration: 2000
    })
  },
  loadunreadNumber() {
    let that = this;
    let id = app.globalData.id || wx.getStorageSync("id");
    if (!id) {
      return false
    }
  },
  getUnreadNews(){//获取未读消息
    let url = '/api/notifications/unreadNumber?sysUserId=' + id;
    requestDataparams(url, {}, 'GET').then(res => {
      if (res.data.code == 0) {

        that.data.items[2].readnum = res.data.data;
        that.setData(that.data);
        if (res.data.data.length == 0) {

        }
      } else {
        // wx.showToast({
        //   title: '网络异常',
        //   icon: 'none'
        // })

      }
    }).catch(err => {
      
    });
  },

  onShow: function () {
    this.loadunreadNumber()
  },
})
